using System;
using System.Collections;
using System.Collections.Generic;
using FrameWork;
using UnityEngine;

/// <summary>
/// MVC Controller层
/// </summary>
public class PlayerController : MonoBehaviour
{
    [Header("玩家UI界面")]
    [SerializeField] private PlayerView playerView;             // 玩家UI界面
    [Header("玩家信息变量")]
    [SerializeField] private PlayerDataSO playerData;
    // [SerializeField] private PlayerLvSO lv;                     // 玩家等级配置文件
    private void Awake()
    {
        // Player.Instance.SetLv(lv);
        Player.Instance.InitInfo();
        Player.Instance.LoadSavaData(playerData);
        
        // 注册玩家数据更新
        EventSystem.Instance.AddListener<float>(EventName.EVENT_PLAYER_INFORMATION_UPDATE_HEALTH,
            Player.Instance.UpdatePlayerHealth);
        EventSystem.Instance.AddListener<float>(EventName.EVENT_PLAYER_INFORMATION_UPDATE_EXP,
            Player.Instance.UpdatePlayerExp);
        EventSystem.Instance.AddListener<float>(EventName.EVENT_PLAYER_INFORMATION_UPDATE_COINS,
            Player.Instance.UpdatePlayerCoins);
    }
    
    //
    private void OnEnable()
    {
        Player.Instance.AddListener(playerView.UpdateView);
        
        // 更新视图
        Player.Instance.UpdateInfo();
    }
    
    //
    private void OnDisable()
    {
        Player.Instance.RemoveListener(playerView.UpdateView);
        
        EventSystem.Instance.RemoveListener<PlayerDataSO>(EventName.EVENT_SAVEDATA_LOAD_PLAYERDATA,
            Player.Instance.LoadSavaData);
        
        // 注册玩家数据更新
        EventSystem.Instance.RemoveListener<float>(EventName.EVENT_PLAYER_INFORMATION_UPDATE_HEALTH,
            Player.Instance.UpdatePlayerHealth);
        EventSystem.Instance.RemoveListener<float>(EventName.EVENT_PLAYER_INFORMATION_UPDATE_EXP,
            Player.Instance.UpdatePlayerExp);
        EventSystem.Instance.RemoveListener<float>(EventName.EVENT_PLAYER_INFORMATION_UPDATE_COINS,
            Player.Instance.UpdatePlayerCoins);
    }
}
